package com.letv.util;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

public final class NumberUtil {

	private static final String[] numArr = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "十三", "十四", "十五", "十六" };

	public static String getCapitalNumber(int num) {
		if (num >= 0 && num <= numArr.length) {
			return numArr[num];
		} else {
			return null;
		}
	}

	public static final int getInt(String numStr, int defaultNum, int maxNum) {
		if (numStr == null || numStr.trim().length() == 0) {
			return defaultNum;
		}
		try {
			int reNum = Integer.valueOf(numStr);
			return maxNum > 0 && reNum > maxNum ? maxNum : reNum;
		} catch (NumberFormatException e) {
			return defaultNum;
		}
	}

	public static final int getInt(String strNum, int defaultNum) {
		return getInt(strNum, defaultNum, 0);
	}

	public static final long getLong(String numStr, long defaultNum) {
		if (numStr == null || numStr.trim().length() == 0) {
			return defaultNum;
		}
		try {
			return Long.valueOf(numStr).longValue();
		} catch (NumberFormatException e) {
			return defaultNum;
		}
	}

	public static final float getFloat(String numStr, float defaultNum) {
		if (numStr == null || numStr.trim().length() == 0) {
			return defaultNum;
		}
		try {
			return Float.valueOf(numStr).floatValue();
		} catch (NumberFormatException e) {
			return defaultNum;
		}
	}
	
	public static final double getDouble(String numStr, double defaultNum) {
		if (numStr == null || numStr.trim().length() == 0) {
			return defaultNum;
		}
		try {
			return Double.valueOf(numStr).doubleValue();
		} catch (NumberFormatException e) {
			return defaultNum;
		}
	}
	
	public static final int getSearchDate(String date) {
		if (date == null) {
			return 0;
		}
		date = StringUtils.remove(date, "-");
		int searchDate = 0;
		try {
			if (date.length() < 5) {
				searchDate = NumberUtils.toInt(date) * 10000;
			} else if (date.length() < 7) {
				searchDate = NumberUtils.toInt(date) * 100;
			} else if (date.length() == 8) {
				searchDate = NumberUtils.toInt(date);
			}
		} catch (Exception e) {
			return 0;
		}
		return searchDate;
	}
}
